﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI;

using LMD.Lib.DataClass;
using LMD.Constant;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;
using LMD.Lib.Controls;

/// <summary>
/// Summary description for MenuManager
/// </summary>
public static class MenuManager
{
    //edited by Rista
    /*static String m_sxGroupMenu =
        "<div class=\"m_navModul\">" +
            "<div class=\"m_pnl_header\" onmouseover=\"expandMenu(this);\">" +
                "<div class=\"m_navSubmodul_header\">" +
                    "{0}</div>" +
                "<div class=\"m_img_fold\">" +
                    "<img src=\"images/nav_lipetan.png\" name=\"imfld\" /></div>" +
            "</div>" +
            "[submenus]" +
        "</div>";*/

    static String m_sxGroupMenu =
        "<div class=\"m_navModul\">" +
            "<div class=\"m_pnl_header\" onclick=\"expandMenu(this);\">" +
                "<div class=\"m_navSubmodul_header\">" +
                    "{0}</div>" +
                "<div class=\"m_img_fold\">" +
                    "<img src=\"images/nav_lipetan.png\" name=\"imfld\" /></div>" +
            "</div>" +
            "[submenus]" +
        "</div>";

    static String m_sxSubMenu =
        "<div class=\"m_navSubmodul_link\" name=\"sbmnu\">" +
            "<a href=\"{0}\">{1}</a></div>";

    public static String GenerateMenu(List<String> lsPrivilledges, String sxPageName, params String[] sxParameters)
    {
        String sxOutput = String.Empty;

        Cached.ICPMenu ccMenu = new Cached.ICPMenu();
        //Edited by Rista
        DataTable dt = ccMenu.GetMenuList(sxPageName);
        //DataTable dt = ccMenu.GetMenuList("Relationship.aspx");

        String sxLastGroupId = String.Empty;
        String sxGroupDiv = String.Empty;
        String sxSubMenus = String.Empty;
        String sxURL = String.Empty;


        foreach (DataRow dr in dt.Rows)
        { 
            String sxCurrentGroupId = dr["P_MENU_GROUP_ID"].ToString();
            String sxGroupTitle = dr["GROUP_TITLE"].ToString();
            String sxItemTitle = dr["ITEM_TITLE"].ToString();
            if (sxParameters.Length > 0)
            {
                sxURL = String.Format(dr["URL"].ToString(), sxParameters);
            }
            else
            {
                sxURL = dr["URL"].ToString();
            }
            if (lsPrivilledges.Contains(sxGroupTitle) && lsPrivilledges.Contains(sxItemTitle))
            {
                if (sxLastGroupId != sxCurrentGroupId)
                {
                    sxOutput += sxGroupDiv.Replace("[submenus]", sxSubMenus);
                    sxGroupDiv = String.Format(m_sxGroupMenu, sxGroupTitle);
                    sxSubMenus = String.Format(m_sxSubMenu, sxURL, sxItemTitle);
                }
                else
                {
                    sxSubMenus += String.Format(m_sxSubMenu, sxURL, sxItemTitle);
                }
                sxLastGroupId = sxCurrentGroupId;
            }
        }
        sxOutput += sxGroupDiv.Replace("[submenus]", sxSubMenus);
		//ambil menu aplikasi lain
        Cached.FeedSource ccSource = new Cached.FeedSource();
        DataTable dt2 = ccSource.GetOtherApps();

        String sxGroupTitle2 = "Aplikasi Lain";
        String sxItemTitle2 = String.Empty;
        sxGroupDiv = String.Format(m_sxGroupMenu, sxGroupTitle2);
        String sxSubMenus2 = String.Empty;
        foreach (DataRow dr2 in dt2.Rows)
        {
            sxURL = dr2["URL"].ToString();
            sxItemTitle2 = dr2["NAME"].ToString();
            sxSubMenus2 += String.Format(m_sxSubMenu, sxURL, sxItemTitle2);
        }
        sxOutput += sxGroupDiv.Replace("[submenus]", sxSubMenus2);
		
        return sxOutput;
    }

    public static String GenerateUserInfoBox(String sxUserId, String sxUserName, DateTime dtmLastLogin)
    {
        String sxOutput = String.Format(
            "<div class=\"m_navModul\">" +
                "<div id=\"m_profileBox\">" +
                    "<div id=\"m_userPic\">" +
                        "<a href=\"NewsFeed.aspx?mode=profile&uid={0}\">" + 
                            "<img src=\"upload/avatar/{0}.jpg\" alt=\"\" onerror=\"setEmptyUserAvatar(this);\" />" + 
                        "</a>" +
                    "</div>" +
                    "<div id=\"m_userName\" class=\"m_pad_l10\">" +
                        "Welcome, {1}" +
                    "</div>" +
                    "<div id=\"m_userLog\" class=\"m_pad_l10\">" +
                        "Your last login was:<br />" +
                        "{2}</span>" +
                    "</div>" +
                "</div>" +
            "</div>",
            sxUserId,
            sxUserName,
            dtmLastLogin.ToString("dddd dd MMM yyyy, HH:mm:ss"));
        return sxOutput;
    }
}
